package com.wlyuan.index.domain.source.valueobj;

import com.wlyuan.core.domain.valueobj.BillId;
import com.wlyuan.core.domain.valueobj.TenantId;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.math.BigDecimal;
import java.util.Optional;
import java.util.function.Function;

@Builder
@AllArgsConstructor
@Getter
@ToString
public class Balance {
    @ApiModelProperty("对账单ID")
    private BillId billId;

    @ApiModelProperty("财务标记 0:未对账,1:加入对账单,2:已对账,3:已结账")
    private BalanceStatus status;

    @ApiModelProperty("预付款已操作 1：已收、2已付、3已收已付")
    private BalanceBit prepayFee;

    @ApiModelProperty("到付款已操作 1：已收、2已付、3已收已付")
    private BalanceBit arrivePayFee;

    @ApiModelProperty("代收款已操作 1：已收、2已付、3已收已付")
    private BalanceBit collectionFee;

    @ApiModelProperty("回执付款已操作 1：已收、2已付、3已收已付")
    private BalanceBit receiptFee;

    @ApiModelProperty("收付款已操作 1：下游结账、2上游结账、3双方结账")
    private BalanceBit paymentFee;

    @ApiModelProperty("租户ID")
    private TenantId tenantId;


    public boolean marked(Function<Balance, BalanceBit> caller, Character character) {
        return Optional.ofNullable(caller.apply(this))
                .map(value -> value.marked(character))
                .orElse(false);
    }

    public BigDecimal getCompletedFee(Quote quote, Character character) {
        if (null == quote) {
            return BigDecimal.ZERO;
        }
        var completedFee = new BigDecimal(0);
        if (null != quote.getCollectionFee() && this.collectionFee.marked(character)) {
            completedFee = completedFee.add(quote.getCollectionFee());
        }
        if (null != quote.getArrivePayFee() && this.arrivePayFee.marked(character)) {
            completedFee = completedFee.add(quote.getArrivePayFee());
        }
        if (null != quote.getPrepayFee() && this.prepayFee.marked(character)) {
            completedFee = completedFee.add(quote.getPrepayFee());
        }
        if (null != quote.getReceiptFee() && this.receiptFee.marked(character)) {
            completedFee = completedFee.add(quote.getReceiptFee());
        }
        return completedFee;
    }
}
